<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Contract\ContractStatus;
use Illuminate\Database\Eloquent\SoftDeletes;

class House extends Model
{
    use HasFactory, SoftDeletes;

    public $table = 'house';

    public $guarded = [];

    public $appends = ['rent_num_name'];

    public function contract()
    {
        return $this->hasOne(Contract::class)->where('status', '<=', 20)->latest();
    }


    public function fastContract()
    {
        return $this->hasOne(Contract::class)->where('status', '=', 15)->latest();
    }
    public function business()
    {
        return $this->belongsTo(Business::class);
    }


    public function houseClass()
    {
        return $this->belongsTo(HouseClass::class, 'id','house_class_id');
    }

    public function device()
    {
        return $this->hasMany(Device::class);
    }

    public function lock()
    {
        return $this->hasMany(TtlockData::class);
    }

    public function dianbiao()
    {
        return $this->hasOne(Device::class)->where('type', 1);
    }

    public function getStatusNameAttribute()
    {
        // 1签订中，10待签约，20已签约，30已退房
        $status = '未出租';
        if ($this->contract) {
            if ($this->contract->status == ContractStatus::QIANDINGZHONG) {
                return '签订中';
            }
            if ($this->contract->status == ContractStatus::DAIQIANYUE) {
                return '签订中';
            }
            if ($this->contract->status == ContractStatus::YIQIANYUE && $this->contract->expired_at < now()->toDateString()) {
                return '租期已过';
            }
            if ($this->contract->status == ContractStatus::YIQIANYUE && $this->contract->expired_at > now()->toDateString() && $this->contract->expired_at < now()->addDays(30)->toDateString()) {
                return '合同快到期';
            }
            if ($this->contract->status == ContractStatus::YIQIANYUE) {
                return '已出租';
            }

        }
        return $status;
    }

    public function getRentNumNameAttribute()
    {
        switch ($this->rent_num) {
            case 1:
                return '月付';
            case 3:
                return '季付';
            case 6:
                return '半年付';
        }
        return '-';
    }

    public function getTenantNameAttribute()
    {
        if ($this->contract) {
            $tenant = $this->contract->contractTenant->where('is_master', 1)->first();
            return $tenant->name ?? '';
        }
        return '';
    }

    public function getTenantMobileAttribute()
    {
        if ($this->contract) {
            $tenant = $this->contract->contractTenant->where('is_master', 1)->first();
            return $tenant->mobile ?? '';
        }
        return '';
    }

    public function getMasterTenantAttribute()
    {
        if ($this->contract) {
            $tenant = $this->contract->contractTenant()->where('is_master', 1)->first();
            return $tenant;
        }
        return null;
    }

    public function getTenantCountAttribute()
    {
        if ($this->contract) {
            return $this->contract->contractTenant()->count();
        }
        return 0;
    }

    public function getDianfeiAttribute()
    {
        $device = $this->device->where('type', 1)->first();
        if ($device) {
            return $device->price;
        }
        return 0;
    }

    public function getContractExpiredAtAttribute()
    {
        if ($this->contract) {
            return $this->contract->expired_at;
        }
        return '';
    }
}
